<template>
  <scroll-view>
    <div class="wrap">
      <div class="box">
        <div class="title">
          {{ NotifyInfo?.title }}
        </div>
        <div class="sendTime">
          发布于:{{ NotifyInfo?.sendTime }}
        </div>
        <div class="content">
          <u-parse :content="NotifyInfo?.content" />
        </div>
        <div v-if="attachment.length && attachment[0] !== ''" class="attachment">
          <div class="left">附件:</div>
          <div class="right">
            <div
              v-for="(d, i) in attachment"
              :key="i"
              class="item_box"
            >
              <div class="item">{{ d }} </div>
              <div class="download" @tap="download(path[i])">点击下载</div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="NotifyInfo?.needReceipt" class="Receiptcontent">
        回复内容:
        <div class="Details">
          {{ NotifyInfo?.receiptContent }}
        </div>
        <div class="input">
          <Input v-model:value="form.Receiptcontent" height="260rpx" placeholder="请输入回执内容" type="textarea" />
        </div>
        <div>
          <div :class="['btn', !canSubmit ? 'btn-disabled' : '']" @tap="sendReceipt">发送回执消息</div>
        </div>
      </div>
      <div v-if="NotifyInfo?.hasReceiptContent" class="Receiptcontent">
        回复内容:
        <div class="Details">
          {{ NotifyInfo?.receiptContent }}
        </div>
      </div>
    </div>
  </scroll-view>
</template>

<script lang="ts" setup>
import { computed, reactive, ref } from 'vue'
import { onLoad, onShow, Router } from '@U/common'
import { weixinSubscription } from '@U/Auth'
import Input from '@C/Input.vue'
import { Msg } from '@C/Msg'
import download from '@U/download'
import { addReceiptContent, getNotifyDetails, getReadNotifyMsg } from './request'
import type { IObj } from '@/interface'

const id = ref('')
const show = ref(false) // 真机存在多次触发的问题，加一层判断
onLoad(() => {
  if (show.value) return
  show.value = true
  id.value = Router.route().params?.notifyId
  getInfoFn()
  readNotifyMsg()
})

// 获取详情信息
const NotifyInfo = ref(<IObj>{})
// 附件
const attachment = ref(<Array<string>>[])
const path = ref()
async function getInfoFn() {
  const { data } = await getNotifyDetails({ notifyId: id.value }).finally(() => {})
  NotifyInfo.value = data
  attachment.value = data.attachmentName.split(';')
  path.value = data.attachment.split(',')
}

// 是否可以提交
const canSubmit = computed(() => (!form.Receiptcontent ? false : form.Receiptcontent))

// 回执消息
const form = reactive({
  Receiptcontent: '',
})

const flag = ref(true)
// 发送回执消息
const sendReceipt = async () => {
  weixinSubscription()
  if (form.Receiptcontent.length < 1) return
  if (!canSubmit.value) return false
  if (flag.value) {
    const { code } = await addReceiptContent({
      notifyId: id.value,
      content: form.Receiptcontent,
    }).finally(() => {})
    if (code === 200) {
      await getInfoFn()
      Msg({ msg: '回执完成' })
    }
  }
  flag.value = false
}
// 消息已读
async function readNotifyMsg() {
  await getReadNotifyMsg({
    notifyId: id.value,
  }).finally(() => {})
}
onShow(() => {
  weixinSubscription()
})
</script>

<style lang="less" scoped>
.wrap {
  background: #FFFFFF;
  .box {
    padding: 0 20rpx 50rpx;
  }
  .title {
    font-weight: 600;
    font-size: 40rpx;
    margin-top: 20rpx;
  }
  .sendTime {
    font-size: 30rpx;
    color: #919191;
    padding: 30rpx 0;
  }
  .content {
    word-wrap: break-word;
    font-size: 35rpx;
  }
  .attachment {
    display: flex;
    margin: 100rpx 0;
    .left {
      margin-top: 10rpx;
    }
    .right {
      font-size: 30rpx;
      margin-left: 20rpx;
      .item_box {
        display: flex;
        align-items: center;
        margin-top: 10rpx;
        .item {
          width: 200rpx;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        .download {
          color: #ff8002;
          margin-left: 5rpx;
        }
      }

    }
  }
  .input {
    width: 686rpx;
    height: 400rpx;
    background: #fff;
    border-radius: 24rpx;
    margin: 0 auto;
  }
  .Receiptcontent {
    width: 100%;
    background: #F5F5F5;
    padding: 30rpx 20rpx;
    .Details {
      margin: 40rpx 0 60rpx;
    }
  }
  .btn {
    width: 100%;
    height: 80rpx;
    font-size: 36rpx;
    border-radius: 46rpx;
    background: #ff8002;
    text-align: center;
    line-height: 80rpx;
    color: #fff;
    margin: 30rpx 0;
  }
  .btn-disabled {
    opacity: 0.65;
    &:active {
      filter: none;
    }
  }
}
</style>
